//
//  MenuTab.m
//  Test
//
//  Created by zhangtie on 13-4-11.
//  Copyright (c) 2013年 zhangtie. All rights reserved.
//

#import "MenuTab.h"

@implementation MenuTab


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
//        [self initImageSign];
        [self setBackgroundColor:[UIColor getColor:@"FCFCFC"]];
        self.padding_bottom = 0;
        self.font_size      = 15;
    }
    return self;
}

- (void)initImageSign
{
    if(!_imageSign)
    {
        _imageSign = [[UIImageView alloc]initWithImage:IMGNAMED(@"sign.png")];
        [_view_scroll addSubview:_imageSign];
//        SIMPLE_SAFERELEASE(_imageSign);
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    REMOVEALLSUBVIEW(self);

    
    //初始化容器
    [self initContainer];
    
    if(_imageSign)
    {
        [_imageSign removeFromSuperview];
        RELEASE_SAFELY(_imageSign);
    }
    if(_view_scroll)
    {
        [_view_scroll removeFromSuperview];
        RELEASE_SAFELY(_view_scroll);
    }
    
    if(!_dataSource || [_dataSource count]<=0)
        return;

    
    NSInteger ndataCount = [_dataSource count];
    
    [self initScrollView];
    
    [self initImageSign];
//    NSInteger nspanCount = ndataCount - 1;
    
    //计算每个tabItem的frame,目前可以滑动,因此改为定宽
//    CGFloat totalWidth = self.frame.size.width;
//    CGFloat item_width = floorf(totalWidth / ndataCount);//106.63f;//
    
    //计算每个tabItem的frame
    CGFloat totalWidth = self.frame.size.width;
    CGFloat item_width = 0;
    if(self.isCoverage)
        item_width = (totalWidth - _itemSpace*(ndataCount + 1))/ndataCount;//floorf((totalWidth - _itemSpace*(ndataCount + 1))/ndataCount);
    else
        item_width = self.itemWidth;
    
    CGFloat item_orginX = 0;
    CGFloat item_orginY = 0.0f;
    CGFloat item_height = self.frame.size.height - item_orginY*2;
    CGFloat title_fontSize = self.font_size;
    
    CGFloat content_width = 0.0f;
    
    for (NSInteger i=0; i<ndataCount; ++i)
    {
        CGFloat width       = 0;
        CustomTabItem_DataItem *data_item = [_dataSource objectAtIndex:i];
        NSString* str_title = [self getTitleForItem:data_item];
        if(item_width == 0)
        {
            CGSize textSize = [str_title sizeWithFont:FONT_TITLE(title_fontSize)];
            width           = textSize.width + 22;
        }
        else
        {
            width           = item_width;
        }

        CGRect item_frame   = CGRectMake(item_orginX, item_orginY, width, item_height);
        CustomTabItem* item = _ALLOC_OBJ_WITHFRAME_(CustomTabItem, item_frame);
        item.color_normal   = self.color_normal;//[QBUtil getColor:@"FCFCFC"];
        item.color_selected = self.color_selected; //[QBUtil getColor:@"FCFCFC"];
        if(!self.color_textSelected)
        {
            item.color_textSelected = [UIColor orangeColor];
        }
        else
        {
            item.color_textSelected = self.color_textSelected;
        }
        if(!self.color_textNormal)
        {
            item.color_textNormal   = [UIColor lightGrayColor];
        }
        else
        {
            item.color_textNormal   = self.color_textNormal;
        }
        [item setFontSize:title_fontSize];
        item.tabItem_delegate = self;
        [item setIndex:i];
        [_view_scroll addSubview:item];
        
        //设置标题
        if(str_title)
            [item setTabItemTitle:str_title];
        
        //设置默认选中项
        if(i == _ndefaultSelectedIndex)
        {
            [item changeForState:TABITEM_STATE_SELECTED];
            [self setSignImageCenterForItem:item];
            if(self.isNeedDefaultSelectedEvent)
            {
                [self toNextResponderWithObject:item];
            }
        }
        else
            [item changeForState:TABITEM_STATE_NORMAL];
        //添加到容器中
        [_container addObject:item];
        content_width += width;
        //添加分隔线
        if(i != (ndataCount-1))
        {//不是最后就不添加分隔
            CGSize fontSize = [str_title sizeWithFont:FONT_TITLE(title_fontSize)];
            CGFloat span_height = fontSize.height - 5;
            CGFloat span_orginX = item_frame.origin.x + item_frame.size.width - SPAN_WIDTH;
            CGFloat span_orginY = (item_frame.size.height - span_height)/2;
            CGFloat span_width  = SPAN_WIDTH;
            CGRect span_frame = CGRectMake(span_orginX, span_orginY, span_width, span_height);
            UILabel *lab_span = _ALLOC_OBJ_WITHFRAME_(UILabel, span_frame);
            [lab_span setBackgroundColor:[UIColor lightGrayColor]];
            [self addSubview:lab_span];
            SIMPLE_SAFERELEASE(lab_span);
        }
        
        item_orginX         += width;
//        else
//        {
//            content_width += SPAN_WIDTH;
//        }
    }
    
     _view_scroll.contentSize = CGSizeMake(content_width, _view_scroll.frame.size.height);
    
//    CGFloat line_width = 1.0f;
//    CGRect top_frame = CGRectMake(0, 0, self.frame.size.width, line_width);
//    UILabel *span_top = [[UILabel alloc]initWithFrame:top_frame];
//    [span_top setBackgroundColor:[UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f]];
//    [self addSubview:span_top];
//    SIMPLE_SAFERELEASE(span_top);
    
//    UIImage *image       = IMGNAMED(@"bg_menu_bottom.png");
//    CGSize imageSize     = image.size;
//    CGRect bottom_frame  = CGRectMake(0, self.frame.size.height - imageSize.height, imageSize.width, imageSize.height);
//    UIImageView *viewImage = [[UIImageView alloc]initWithFrame:bottom_frame];
//    [viewImage setImage:image];
////    UILabel *span_bottom = [[UILabel alloc]initWithFrame:bottom_frame];
////    [span_bottom setBackgroundColor:[UIColor colorWithPatternImage:image]];
//    [self addSubview:viewImage];
//    SIMPLE_SAFERELEASE(viewImage);
}

- (void)setSignImageCenterForItem:(CustomTabItem*)item
{
    //    _imageSign.center = item.center;
    
    [self setSignImageCenterForItem:item animal:NO];
}

- (void)setSignImageCenterForItem:(CustomTabItem*)item animal:(BOOL)animal
{
    if(!_imageSign)
        [self initImageSign];
    
    UIImage *image       = IMGNAMED(@"bg_menu_bottom.png");
    CGSize imageSize     = image.size;
    if(animal)
    {
        _ANIMATIONS_INIT_BEGIN_(0.3);
        _imageSign.center = CGPointMake(item.center.x, item.frame.size.height - _imageSign.frame.size.height - imageSize.height + 7);
        _ANIMATIONS_INIT_END_;
    }
    else
    {
        _imageSign.center = CGPointMake(item.center.x, item.frame.size.height - _imageSign.frame.size.height - imageSize.height + 7);
    }
    [_view_scroll bringSubviewToFront:_imageSign];
}

#pragma mark -- TabItemDelegate
- (void)doTabItemClick:(id)sender
{
    if(!_imageSign)
        [self initImageSign];
    
    CustomTabItem* cur_item = (CustomTabItem*)sender;
    
    if(_ncurrentSelectedIndex == cur_item.nindex)//过滤操作
        return;
    
    if(cur_item)
    {
         _ncurrentSelectedIndex = cur_item.nindex;
        [cur_item changeForState:TABITEM_STATE_SELECTED];
        //        CGRect current_frame = cur_item.frame;
        [self setSignImageCenterForItem:cur_item animal:_changeAnimal];
    }
    for(CustomTabItem* item in _container)
    {
        if(item == cur_item)
            continue;
        
        [item changeForState:TABITEM_STATE_NORMAL];
    }
    
    [self toNextResponderWithObject:cur_item];
}

@end
